﻿#include "HWBaseDialog.h"
#include "services/tlv/Com/TlvThreadCom.h"
#include "components/Toast/Toast.h"


HWBaseDialog::HWBaseDialog(QWidget *parent)
    : FDialog{parent}
{
    connect(TlvThreadCom::instance(), &TlvThreadCom::errorOccurred, this,  [this](const QString& err){
        Q_UNUSED(err);
        setUIEnabled(true);
    });
}

bool HWBaseDialog::changeWorker(ICom *com)
{
    TlvThreadCom* ttCom = TlvThreadCom::instance();
    bool isSame = (ttCom->hwName() == com->hwName());
    bool isOpen = ttCom->isOpen();

    if (isOpen && isSame) {
        ttCom->close();
        setUIEnabled(true);
        return false;
    } else if (isOpen && !isSame) {  // 其它设备已打开，需先关闭
        Toast::warning(tr("close [%1] please.").arg(ttCom->hwName()));
        return false;
    } else if (!isOpen && isSame) {
        return true;
    } else {
        ttCom->setWorker(com);
        return true;
    }
}
